<template>
  <div>
    <div class="data">
      <div>
        <!-- 搜索框 -->
        <el-form :inline="true" :model="params" class="demo-form-inline">
          <el-form-item label="名称">
            <el-input
              v-model="params.name"
              placeholder="按群组名称查找"
              class="block"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="searchList"
              class="block"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="message">
      <div class="message1">
        <!-- 列表 -->
        <el-table :data="rows" stripe style="width: 100%">
          <el-table-column prop="name" label="名称" width="180">
          </el-table-column>
          <el-table-column prop="desoptions" label="描述">
          </el-table-column>
          <el-table-column prop="status" label="是否激活" width="180">
            <template slot-scope="scope">
              <!-- 切换状态 -->
              <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="changeStatus(scope.row)">
              </el-switch>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="pagination">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="params.pageNum"
        :page-sizes="[5,10]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "groupList",
  components: {},
  data() {
    return {
      loading: false,
      params: {//请求参数
        pageSize: 10,//分页大小
        pageNum: 1,//当前页
        name:'',//按名字查询
        userId:''//用户ID
      },
      total: 0,//数据总数
      rows: []//返回数据
    }
  },
  methods: {
    changeStatus(row){
      if(row.status == 1){//新增用户群组(权限)
        this.$http({
          url:'/v1/groupUser',
          method:'POST',
          data:{
            userId:this.params.userId,//userId 是通过父组件调用getList传入  保存在params里面
            groupId:row._id
          }
        })
      }else{//删除用户群组(权限)
        this.$http({
          url:'/v1/groupUser',
          method:'DELETE',
          params:{
            userId:this.params.userId,//userId 是通过父组件调用getList传入  保存在params里面
            groupId:row._id
          }
        })
      }
    },
    sizeChange(pageSize) {
      //修改分页大小
      this.params.pageSize = pageSize;
      //重置到第一页
      this.params.pageNum = 1;
      //获取数据
      this.getList();
    },
    currentChange(pageNum) {
      //跳转到第几页
      this.params.pageNum = pageNum;
      //获取数据
      this.getList();
    },
    searchList() {
      //重置到第一页
      this.params.pageNum = 1;
      //获取数据
      this.getList();
    },
    getList(userId) {
      //只有父组件调用的时候 保存userId
      if(userId) this.params.userId = userId
      this.loading = true;
      //获取数据
      this.$http({
        url: "/v1/groupUser",
        method: "GET",
        params: this.params,
      }).then((res) => {
        this.loading = false
        //设置返回总数
        this.total = res.total
        //设置列表数据
        this.rows = res.rows
      })
    }
  }
}
</script>

<style scoped>

</style>
